//获取应用实例
const app = getApp();
var utilMd5 = require('../util/md5.js');

Page({
	data: {
    // 服务选择
    isOne:true,
		idx:'',
		applyList:[
			{Item_id: "1", Item_Name: "92#"},
			{Item_id: "2", Item_Name: "95#"}],
		idx1:'',
		applyList1:[
			{Item_id: "1", Item_Name: "1号"},
			{Item_id: "2", Item_Name: "2号"},
			{Item_id: "4", Item_Name: "4号"},
			{Item_id: "5", Item_Name: "5号"},
			{Item_id: "8", Item_Name: "8号"},
			{Item_id: "11", Item_Name: "11号"}],
		idx2:'',
		applyList2:[
			{Item_id: "3", Item_Name: "3号"},
			{Item_id: "6", Item_Name: "6号"},
			{Item_id: "9", Item_Name: "9号"},
			{Item_id: "12", Item_Name: "12号"}],
		jyList:[],
		gasId:'',
		gunId:'',//选中的枪号
		imgs:'',
		address:'',
		list:[],
		oilPriceList:[],
		checkPrice:'',
  },
	onLoad: function(options) {
		this.getlist(options.id);
		this.setData({
			gasId:options.id,
			address:options.addres,
			imgs:options.img,
		})
		console.log(this.data.address)
		console.log(this.data.imgs)
	},
	
	selectApply:function(e){
		let id = e.target.dataset.id
		console.log(id)
		this.setData({
			idx: id,
			gunId:id
		})
		if(id==92){
			this.setData({
				isOne:true
			})
		}else{
			this.setData({
				isOne:false
			})
		}
	},
	selectApply1:function(e){
		let id = e.target.dataset.id
		this.setData({
			idx1: id,
			gunId:id,
			checkPrice:this.data.oilPriceList[0].priceYfq
		})
		console.log(this.data.gunId)
	},
	selectApply2:function(e){
		let id = e.target.dataset.id
		this.setData({
			idx2: id,
			gunId:id,
			checkPrice:this.data.oilPriceList[1].priceYfq
		})
		console.log(this.data.gunId)
	},

	//获取加油站详情
	getlist:function(id){
		var that=this;
		console.log(id)
    wx.request({
      url: app.globalData.apiJy + '/app.php?c=Gas&a=getGasDetail',
      data: {
				phone: app.globalData.phone,
				gasIds:id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
				wx.hideLoading();
				for (var i = 0; i < res.data.data.list[0].oilPriceList.length; i++) {
					if (res.data.data.list[0].oilPriceList[i].oliNo == 0) {
						res.data.data.list[0].oilPriceList.splice(i, 1);
					}
					// else if (res.data.data.list[i].oliNo == -10) {
					// 	res.data.data.list.splice(i, 1);
					// }
				}
        that.setData({
					jyList:res.data.data.list,
					oilPriceList:res.data.data.list[0].oilPriceList,
					checkPrice:res.data.data.list[0].oilPriceList[0].priceYfq,
					applyList:res.data.data.list[0].oilPriceList,
					applyList1:res.data.data.list[0].oilPriceList[0].gunNos,
					applyList2:res.data.data.list[0].oilPriceList[1].gunNos,
        })
      },
      fail: function () {
        wx.hideLoading();
      }
    });
	},
	
	getCode:function(){
		var that=this;
		var timestamp = Date.parse(new Date());
		timestamp = timestamp / 1000;
		console.log(timestamp)
		var code = '537f9844eb30bd11e49e0fe702dd1e68' + 'app_keyappm_api_h592655472'+'phone'+app.globalData.phone + 'platformId92655472timestamp' + timestamp+'000' +'537f9844eb30bd11e49e0fe702dd1e68';
		console.log(code)
		var MD5code = utilMd5.md5(code);   //code需要加密的数据
		console.log(MD5code)
		wx.request({
			url: 'https://mcs.czb365.com/services/v3/begin/getSecretCode',
			data:{
				app_key:'appm_api_h592655472',
				platformId:'92655472',
				timestamp:''+timestamp+'000',
				phone:''+app.globalData.phone,
				sign:''+MD5code
			},
			header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
				console.log(res);
				console.log(res.data.result)
				console.log('https://open.czb365.com/redirection/todo/?platformType=92655472&authCode='+res.data.result+'&gasId='+that.data.gasId+'&gunNo='+that.data.gunId)
				wx.setClipboardData({
					data: 'https://open.czb365.com/redirection/todo/?platformType=92655472&authCode='+res.data.result+'&gasId='+that.data.gasId+'&gunNo='+that.data.gunId,
          success: function (res) {
						
            wx.getClipboardData({
              success: function (res) {
                wx.showToast({
									title: '已复制该链接，请到浏览器中进行支付',
									icon:'none',
									duration:2000
								})
              }
            })
          }
        })
				
      },
      fail: function () {
        wx.hideLoading();
      }
		})
	}
});
